/*
 * jxUtils Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxUtils.security;

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxUtils
 * @package org.jxUtils.security
 * @date 21/06/2014 - 09:10:23
 */
public final class SecurityUtil {
	
	/**
	 * @param value
	 * @param key
	 * @return decryptedValue
	 * @throws Throwable
	 */
	public static String fromDES(final String value, final SecretKey key) throws Throwable {
	
		final Cipher cipher = Cipher.getInstance("DES");
		
		cipher.init(Cipher.ENCRYPT_MODE, key);
		
		return new String(cipher.doFinal(new BASE64Decoder().decodeBuffer(value)), "UTF8");
	}
	
	/**
	 * @param encryptedValue
	 * @param privateKey
	 * @return value
	 * @throws Throwable
	 */
	public static byte[] fromRSA(final byte[] encryptedValue, final PrivateKey privateKey) throws Throwable {
	
		final Cipher cipher = Cipher.getInstance("RSA");
		
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		
		return cipher.doFinal(encryptedValue);
	}
	
	/**
	 * @return {@link SecretKey}
	 * @throws Throwable
	 */
	public static SecretKey generateDESSecretKey() throws Throwable {
	
		return KeyGenerator.getInstance("DES").generateKey();
	}
	
	/**
	 * @return {@link KeyPair}
	 * @throws Throwable
	 */
	public static KeyPair generateRSAKeyPair() throws Throwable {
	
		return generateRSAKeyPair(512);
	}
	
	/**
	 * @param keySize
	 * @return {@link KeyPair}
	 * @throws Throwable
	 */
	public static KeyPair generateRSAKeyPair(final int keySize) throws Throwable {
	
		final SecureRandom random = new SecureRandom();
		
		final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
		
		generator.initialize(keySize, random);
		
		return generator.generateKeyPair();
	}
	
	/**
	 * @param value
	 * @param key
	 * @return encryptedValue
	 * @throws Throwable
	 */
	public static String toDES(final String value, final SecretKey key) throws Throwable {
	
		final Cipher cipher = Cipher.getInstance("DES");
		
		cipher.init(Cipher.ENCRYPT_MODE, key);
		
		return new BASE64Encoder().encode(cipher.doFinal(value.getBytes("UTF8")));
	}
	
	/**
	 * @param value
	 * @return encryptedValue
	 * @throws NoSuchAlgorithmException
	 */
	public static String toMD5(final String value) throws NoSuchAlgorithmException {
	
		final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
		
		messageDigest.update(value.getBytes(), 0, value.length());
		
		return String.valueOf(new BigInteger(1, messageDigest.digest()).toString(16));
	}
	
	/**
	 * @param value
	 * @return {@code Map<KeyPair, EncryptedValue>}
	 * @throws Throwable
	 */
	public static Map<KeyPair, byte[]> toRSA(final byte[] value) throws Throwable {
	
		return toRSA(value, 512);
	}
	
	/**
	 * @param value
	 * @param keySize
	 *        >= 512
	 * @return {@code Map<KeyPair, EncryptedValue>}
	 * @throws Throwable
	 */
	public static Map<KeyPair, byte[]> toRSA(final byte[] value, final int keySize) throws Throwable {
	
		final Map<KeyPair, byte[]> result = new HashMap<>();
		
		final KeyPair keyPair = generateRSAKeyPair(keySize);
		
		final PublicKey publicKey = keyPair.getPublic();
		
		final byte[] encryptedValue = toRSA(value, publicKey);
		
		result.put(keyPair, encryptedValue);
		
		return result;
	}
	
	/**
	 * @param value
	 * @param publicKey
	 * @return EncryptedValue
	 * @throws Throwable
	 */
	public static byte[] toRSA(final byte[] value, final PublicKey publicKey) throws Throwable {
	
		final SecureRandom random = new SecureRandom();
		
		final Cipher cipher = Cipher.getInstance("RSA");
		
		cipher.init(Cipher.ENCRYPT_MODE, publicKey, random);
		
		return cipher.doFinal(value);
	}
}
